<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/2/28
 * Time: 15:20
 */

namespace App\Http\Logics\MiniProgramAdmin;


use App\Category;
use App\Http\Logics\BaseLogic;

class CategoryLogic extends BaseLogic
{
    public static function getCategoryList($data)
    {
        $category = Category::where(function ($query) use ($data){
            $query->where('parent_id', $data['parent_id']);
            if (isset($data['status']) && $data['status'] != ''){
                $query->where('status', $data['status']);
            }
        })->orderBy('sort')->get();
        if (empty($category)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $category->map(function ($category) {
            return $category->append('status_bool');
        });
        return self::response(self::SUCCESS, $category);
    }

    public static function setSort($data)
    {
        $new_sort = explode(',', $data['new_sort_str']);

        foreach ($new_sort as $key => $item){
            $category = Category::find($item);
            if (empty($category)){
                return self::response(self::CONTENT_IS_EMPTY);
            }
            $category->sort = $key;
            if (!$category->save()){
                return self::response(self::OBJECT_SAVE_FAIL);
            }
        }
        return self::response(self::SUCCESS);
    }

    public static function createCategory($data)
    {
        $category = new Category();
        $category->parent_id = $data['parent_id'];
        $category->name = $data['name'];
        $category->icon = $data['icon'];
        if (!$category->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    /**
     * 上传城市封面
     * @param $data
     */
    public static function uploadIcon($data)
    {
        try{
            $url = uploadImage(storage_path().'/app/'.$data['icon_file'], 'city-banner');
            return self::response(self::SUCCESS, $url);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    public static function deleteCategory($data)
    {
        $category = Category::find($data['category_id']);
        if (empty($category)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        if (!$category->delete()){
            return self::response(self::OBJECT_DELETE_FAILD);
        }
        return self::response(self::SUCCESS);
    }

    public static function updateCategory($data)
    {
        $category = Category::find($data['category_id']);
        if (empty($category)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $category->name = $data['name'];
        $category->icon = $data['icon'];
        if (!$category->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    public static function getCategoryDetail($data)
    {
        $category = Category::find($data['category_id']);
        if (empty($category)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $category);
    }

    public static function changeStatus($data)
    {
        try{
            $category = Category::find($data['category_id']);
            if (empty($category)){
                return self::response(self::CONTENT_IS_EMPTY);
            }
            $category->status = !$category->status;
            if (!$category->save()){
                return self::response(self::OBJECT_SAVE_FAIL);
            }
            return self::response(self::SUCCESS);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }
}